package com.example.composableexample.animate

import androidx.compose.animation.core.LinearEasing
import androidx.compose.animation.core.animateFloatAsState
import androidx.compose.animation.core.repeatable
import androidx.compose.animation.core.tween
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.BoxScope
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.RowScope
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Button
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.blur
import androidx.compose.ui.draw.clip
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.TileMode
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import kotlinx.coroutines.delay

private val shineColors = listOf(
    Color.White,
    Color(0xFF00B0FF)
)

@Composable
fun AnimatedBorderCard(
    modifier: Modifier = Modifier,
    content: @Composable BoxScope.() -> Unit
) {
    val containerSize = 200.dp
    var offsetFloat by remember {
        mutableStateOf(0f)
    }
    LaunchedEffect(null) {
        delay(100)
        offsetFloat = containerSize.value * 10f
    }
    val offset by animateFloatAsState(
        targetValue = offsetFloat,
        animationSpec = repeatable(
            iterations = 4,
            animation = tween(durationMillis = 3000, easing = LinearEasing)
        )
    )
    val brush = Brush.linearGradient(
        colors = shineColors,
        start = Offset(offset, offset),
        end = Offset(offset + containerSize.value * 40, offset + containerSize.value * 40),
        tileMode = TileMode.Repeated
    )

    Box(
        modifier = modifier
            .clip(RoundedCornerShape(24.dp))
            .border(width = 2.5.dp, brush = brush, shape = RoundedCornerShape(24.dp))
            .background(color = Color(0xffA9A9A9))
//            .blur(radius = 2.dp)
    ) {
        content()
    }

}

@Preview
@Composable
fun AnimatedBorderCardPreview() {
    AnimatedBorderCard(modifier = Modifier.size(500.dp)) {
        Text(text = "居中", modifier = Modifier.align(Alignment.Center))
    }
}